*------------------------------------------------------------------------------*
					*** TABLE S1. Descriptive statistics ***
*------------------------------------------------------------------------------*

***Program to produce table
cap program drop reg_tab1
program define reg_tab1, sclass
	args cmd spec 
	/* ARGUMENTS
	cmd: command (sum or xtreg)
	spec: number of the specification 
	*/

	
	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	
	local Y : word count $vars_tab1 // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $vars_tab1
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSIONS FOR SD WITHIN
		
		// THE FOLLOWING SAVES MEAN, STANDARD DEVIATION, min, MAX AND N AS SCALARS
		scalar mu`spec'_`i'   = r(mean) // mean
		scalar sdt`spec'_`i'  = r(sd) // sd(total)
		scalar min`spec'_`i'  = r(min) // min
		scalar max`spec'_`i'  = r(max) // Max
		scalar N`spec'_`i'    = r(N) // N
		
		
		// THE FOLLOWING SAVES STANDARD DEVIATION OF THE RESIDULAS
		scalar sdw`spec'_`i'  =e(rmse) // sd(within)		
		
	}
end


*------------------------------------------------------------------------------*

***Macros
*Variables
global vars_tab1 "Eligible_voters Valid turnout Incumbent_share Est_opp_share OtherParties_share PM10_30km_10ug O3_30km_10ug temp_30km humid_rel_30km wind_speed_30km precip_30km pop_tot GDP_capita employment NO2_30km_10ug"

*Specifications
global spec1   						//  To restrict sample to period 1990-2013 when weather and ozone are not missing
global spec2 "i.election_date, fe"  // For within s.e.


*------------------------------------------------------------------------------*

use "${tempdata}kreise_complete_all.dta", clear

*------------------------------------------------------------------------------*

keep if samplePM10_allcontrols==1

*------------------------------------------------------------------------------*
*Soft coding nr observations in estimation sample
sum Incumbent_share
local est_sampl = r(N)
local clean_est_sampl: display %4.0f `est_sampl'
display "`est_sampl'"
file open myfile using "${outputs}in_text_numbers_generated_in_stata.txt", write text append
file write myfile "Size of estimation sample: `est_sampl'" _n(1)
file close myfile

*------------------------------------------------------------------------------*
*Soft coding nr of counties in estimation sample
distinct Kreis
local nr_kreis_es = r(ndistinct)
local clean_nr_kreis_es: display %4.0f `nr_kreis_es'
display "`clean_nr_kreis_es'"
file open myfile using "${outputs}in_text_numbers_generated_in_stata.txt", write text append
file write myfile "Number of counties in estimation sample: `clean_nr_kreis_es'" _n(1)
file close myfile

*------------------------------------------------------------------------------*
*Soft coding number of elections in estimation sample
preserve
	keep if election_typeBWorLW == "LW"  // We already know that BW elections are 5
	egen election_land = group(election_date Land)
	distinct election_land
	local nr_elections_es = r(ndistinct) + 5 
	local clean_nr_elections_es: display %4.0f `nr_elections_es'
	display "`clean_nr_elections_es'"
	file open myfile using "${outputs}in_text_numbers_generated_in_stata.txt", write text append
	file write myfile "Number of elections in estimation sample: `clean_nr_elections_es'" _n(1)
	file close myfile
restore

*------------------------------------------------------------------------------*
*Soft coding nr voters and turnout
foreach m in mean min max {

	*Eligible voters
	sum Eligible_voters
	local `m'_voters = round(r(`m')/1000)*1000
	di ``m'_voters'
	local clean_`m'_voters: display %8.0fc ``m'_voters'
	display "`clean_`m'_voters'"
	file open myfile using "${outputs}in_text_numbers_generated_in_stata.txt", write text append
	file write myfile "Eligible voters (`m'): `clean_`m'_voters'" _n(1)
	file close myfile

	*Turnout
	sum turnout
	local `m'_turnout = round(r(`m')*100)
	di ``m'_turnout'
	local clean_`m'_turnout: display %3.0f ``m'_turnout'
	display "`clean_`m'_turnout'"
	file open myfile using "${outputs}in_text_numbers_generated_in_stata.txt", write text append
	file write myfile "Turnout (`m'): `clean_`m'_turnout'%" _n(1)
	file close myfile

} // v

*------------------------------------------------------------------------------*

***Run specifications
**Summary Statistics
qui reg_tab1 sum 1


***Regressions for within SD
qui reg_tab1 xtreg 2

*------------------------------------------------------------------------------*

***Generate descriptive statistics table	
gen str table="" 

**Voting data
local i=1
local line "\ \ Eligible voters &"
local line "`line'" $f_mu_b  mu1_1 "&" $f_sdt_b sdt1_1 "&" $f_sdw_b sdw2_1 "&" $f_min_b min1_1 "&" $f_max_b max1_1 "&" $f_Nc N1_1 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Valid votes &"
local line "`line'" $f_mu_b mu1_2 "&" $f_sdt_b sdt1_2 "&" $f_sdw_b sdw2_2 "&" $f_min_b min1_2 "&" $f_max_b max1_2 "&" $f_Nc N1_2 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Turnout &"
local line "`line'" $f_mu mu1_3 "&" $f_sdt sdt1_3 "&" $f_sdw sdw2_3 "&" $f_min min1_3 "&" $f_max max1_3 "&" $f_Nc N1_3 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Share incumbent parties &"
local line "`line'" $f_mu mu1_4 "&" $f_sdt sdt1_4 "&" $f_sdw sdw2_4 "&" $f_min min1_4 "&" $f_max max1_4 "&" $f_Nc N1_4 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Share established opposition parties &"
local line "`line'" $f_mu mu1_5 "&" $f_sdt sdt1_5 "&" $f_sdw sdw2_5 "&" $f_min min1_5 "&" $f_max max1_5 "&" $f_Nc N1_5 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Share other parties &" $f_mu mu1_6 "&" $f_sdt sdt1_6 "&" $f_sdw sdw2_6 "&" $f_min min1_6 "&" $f_max max1_6 "&" $f_Nc N1_6 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\\"
local line: display "`line'" 
qui replace table="`line'" in `i'


**Pollution data
local i=`i'+1
qui replace table="\multicolumn{7}{l}{\textbf{Pollution data} } \\ " in `i'
local i=`i'+1
local line "\ \ PM10 ($10\mu g/m^3$) &" $f_mu mu1_7 "&" $f_sdt sdt1_7 "&" $f_sdw sdw2_7 "&" $f_min min1_7 "&" $f_max max1_7 "&" $f_Nc N1_7 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Ozone ($10\mu g/m^3$) &" $f_mu mu1_8 "&" $f_sdt sdt1_8 "&" $f_sdw sdw2_8 "&" $f_min min1_8 "&" $f_max max1_8 "&" $f_Nc N1_8 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ NO2 ($10\mu g/m^3$) &" $f_mu mu1_16 "&" $f_sdt sdt1_16 "&" $f_sdw sdw2_16 "&" $f_min min1_16 "&" $f_max max1_16 "&" $f_Nc N1_16 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\\"
local line: display "`line'" 
qui replace table="`line'" in `i'


**Weather data
local i=`i'+1
qui replace table="\multicolumn{7}{l}{\textbf{Weather data} } \\ " in `i'
local i=`i'+1
local line "\ \ Temperature (\textdegree $ C $) &" $f_mu mu1_9 "&" $f_sdt sdt1_9 "&" $f_sdw sdw2_9 "&" $f_min min1_9 "&" $f_max max1_9 "&" $f_Nc N1_9 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Relative humidity (\%) &"
local line "`line'" $f_mu mu1_10 "&" $f_sdt sdt1_10 "&" $f_sdw sdw2_10 "&" $f_min min1_10 "&" $f_max max1_10 "&" $f_Nc N1_10 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Wind speed ($ m/s $) &"
local line "`line'" $f_mu mu1_11 "&" $f_sdt sdt1_11 "&" $f_sdw sdw2_11 "&" $f_min min1_11 "&" $f_max max1_11 "&" $f_Nc N1_11 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Precipitation ($ mm $) &"
local line "`line'" $f_mu mu1_12 "&" $f_sdt sdt1_12 "&" $f_sdw sdw2_12 "&" $f_min min1_12 "&" $f_max max1_12 "&" $f_Nc N1_12 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\\"
local line: display "`line'" 
qui replace table="`line'" in `i'

**Demographic and Economic data
local i=`i'+1
qui replace table="\multicolumn{7}{l}{\textbf{Demographic and economic data} } \\ " in `i'
local i=`i'+1
local line "\ \ Population &" $f_mu_b  mu1_13 "&" $f_sdt_b sdt1_13 "&" $f_sdw_b sdw2_13 "&" $f_min_b min1_13 "&" $f_max_b max1_13 "&" $f_Nc N1_13 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ GDP per capita &" $f_mu_b  mu1_14 "&" $f_sdt_b sdt1_14 "&" $f_sdw_b sdw2_14 "&" $f_min_b min1_14 "&" $f_max_b max1_14 "&" $f_Nc N1_14 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
local line "\ \ Employment rate &" $f_mu mu1_15 "&" $f_sdt sdt1_15 "&" $f_sdw sdw2_15 "&" $f_min min1_15 "&" $f_max max1_15 "&" $f_Nc N1_15 " %\\ "
local line: display "`line'" 
qui replace table="`line'" in `i'


**Create .tex file
outfile table using "${outputs}table_S1.tex" in 1/`i', replace noquote
drop table
macro drop star*

*------------------------------------------------------------------------------*

clear

exit
